/**
 * 
 */
package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author wevjoso
 *
 */
public class PalvMgr extends BaseObj{
	
	public Palv get(String id) {
		clearErr();
		Palv palv = new Palv();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select p.*,case when p.monthin =1 then '是' when p.monthin=2 then '否' end as monthin_name from palv  p where taskid = ?";
			conn.prepare(sql);
			conn.setString(1,id);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			palv.setComid(getInt(rs,0,"comid"));
			palv.setComname(get(rs,0,"comname"));
			palv.setContype(get(rs,0,"contype"));
			palv.setM(getInt(rs,0,"m"));
			palv.setOutefee(getDouble(rs,0,"outefee"));
			palv.setPrjname(get(rs,0,"prjname"));
			palv.setSpartfee(getDouble(rs,0,"spartfee"));
			palv.setTaskid(getInt(rs,0,"taskid"));
			palv.setTripfee(getDouble(rs,0,"tripfee"));
			palv.setUser_id(get(rs,0,"user_id"));
			palv.setUser_name(get(rs,0,"user_name"));
			palv.setMonthin(getInt(rs,0,"monthin"));
			palv.setMonthin_name(get(rs,0,"monthin_name"));
			palv.setY(getInt(rs,0,"y"));
			
			
			
		
			return palv;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		
		try {
			conn = new DbConn();
			String sqlCount = "select count(*) as total from palv  where 1=1";
				
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select  *,case when monthin=1 then '是' when monthin=2 then '否' end as monthin_name  from palv  where 1=1";
					
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			//conn.setInt(1,palv.getYm());
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			Palv palv = new Palv();
			palv.setComid(getInt(rs,i,"comid"));
			palv.setComname(get(rs,i,"comname"));
			palv.setContype(get(rs,i,"contype"));
			//palv.setCosts(getDouble(rs,i,"costs"));
			//palv.setFifee(getDouble(rs,i,"fifee"));
			//palv.setHoldfee(getDouble(rs,i,"holdfee"));
			//palv.setIncome(getDouble(rs,i,"income"));
			//palv.setInfee( getDouble(rs,i,"infee"));
			palv.setM(getInt(rs,i,"m"));
			//palv.setMfee(getDouble(rs,i,"mfee"));
		//	palv.setMyear(getInt(rs,i,"myear"));
			palv.setOutefee(getDouble(rs,i,"outefee"));
			//palv.setId(getDouble(rs,i,"id"));
			palv.setPrjname(get(rs,i,"prjname"));
			palv.setSpartfee(getDouble(rs,i,"spartfee"));
			palv.setTripfee(getDouble(rs,i,"tripfee"));
			palv.setUser_id(get(rs,i,"user_id"));
			palv.setUser_name(get(rs,i,"user_name"));
			palv.setY(getInt(rs,i,"y")); 
			palv.setTaskid(getInt(rs,i,"taskid"));
			palv.setMonthin(getInt(rs,i,"monthin"));
			palv.setPrjname(get(rs,i,"prjname"));
			palv.setMonthin_name(get(rs,i,"monthin_name"));

		
			r.add(palv);
		}
		return r;
	}
	

}
